package ui;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;

import javax.swing.BorderFactory;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JSplitPane;

import sniff.pack.CommonPacket;
import ui.other.YPBorderFactory;
import ui.util.YPUIUtil;
import ui.widget.TxtPanel;
import ui.widget.XPanel;
import ui.widget.YPanel;

public class YPSniffUI extends JFrame implements Runnable {
	private static YPSniffUI self;
	private Dimension size;
	private YPanel simpleMsgPanel;
	private DetailMsgPanel detailMsgPanel;
	private ByteMsgPanel byteMsgPanel;
	private FuncPanel funcPanel;
	private XPanel simpleMsgTitlePanel;
	private SimpleMsgItem oldItem;
	private YPSniffUI() {
		size = new Dimension(1300, 700);
		init();
	}

	private void init() {
		setTitle("简易抓包系统");
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setSize(size);
		setPreferredSize(size);
		setMinimumSize(size);
		setLocation(YPUIUtil.getCenterLocation(size));
		
		simpleMsgPanel=new YPanel();
		detailMsgPanel =new DetailMsgPanel();
		byteMsgPanel = new ByteMsgPanel();
		funcPanel = FuncPanel.getInstance();
		simpleMsgTitlePanel = new XPanel(this.getWidth()-30,30);
		initSimpleMsgTitlePanel();
		
		JScrollPane jScrollPane1 = new JScrollPane(simpleMsgPanel, JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED,
					JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
		JScrollPane jScrollPane2 = new JScrollPane(detailMsgPanel, JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED,
					JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
		JScrollPane jScrollPane3 = new JScrollPane(byteMsgPanel, JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED,
				JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
		
		JPanel pane1 = new YPanel();
		pane1.add(simpleMsgTitlePanel);
		pane1.add(jScrollPane1);
		
		jScrollPane1.getViewport().setOpaque(false);
		jScrollPane2.getViewport().setOpaque(false);
		jScrollPane3.getViewport().setOpaque(false);
		jScrollPane1.setViewportBorder(BorderFactory.createEmptyBorder());
		jScrollPane1.getVerticalScrollBar().setUnitIncrement(20);
		jScrollPane1.getVerticalScrollBar().setPreferredSize(new Dimension(5, 600));
		jScrollPane2.setViewportBorder(BorderFactory.createEmptyBorder());
		jScrollPane2.getVerticalScrollBar().setUnitIncrement(20);
		jScrollPane2.getVerticalScrollBar().setPreferredSize(new Dimension(5, 600));
		jScrollPane3.setViewportBorder(BorderFactory.createEmptyBorder());
		jScrollPane3.getVerticalScrollBar().setUnitIncrement(20);
		jScrollPane3.getVerticalScrollBar().setPreferredSize(new Dimension(5, 600));
		
		
		
		JSplitPane southSplitPane = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT, jScrollPane2, jScrollPane3);
		JSplitPane mainSplitPane = new JSplitPane(JSplitPane.VERTICAL_SPLIT, pane1, southSplitPane);
		mainSplitPane.setOpaque(false);
		mainSplitPane.setBorder(BorderFactory.createEmptyBorder());
		mainSplitPane.setDividerSize(2);
		mainSplitPane.setDividerLocation(0.5);
		mainSplitPane.setResizeWeight(0.5);
		southSplitPane.setOpaque(false);
		southSplitPane.setBorder(BorderFactory.createEmptyBorder());
		southSplitPane.setDividerLocation(0.5);
		southSplitPane.setResizeWeight(0.5);
		southSplitPane.setDividerSize(2);
		
		JPanel contentPane = new JPanel();
		contentPane.setLayout(new BorderLayout());
		contentPane.add(funcPanel,BorderLayout.NORTH);
		contentPane.add(mainSplitPane,BorderLayout.CENTER);
		contentPane.setBackground(Color.white);
		setContentPane(contentPane);
	}
	
	private void initSimpleMsgTitlePanel() {
		simpleMsgTitlePanel.add(new TxtPanel(80,30,"No."));
		simpleMsgTitlePanel.add(new TxtPanel(150,30,"Date"));
		simpleMsgTitlePanel.add(new TxtPanel(280,30,"Source"));
		simpleMsgTitlePanel.add(new TxtPanel(280,30,"Destination"));
		simpleMsgTitlePanel.add(new TxtPanel(100,30,"Protocol"));
		simpleMsgTitlePanel.add(new TxtPanel(70,30,"Length"));
		simpleMsgTitlePanel.add(new TxtPanel(300,30,"Info"));
		simpleMsgTitlePanel.setBorder(YPBorderFactory.getUnderLineBorder());
	}
	
	public void addPackSimpleMsg(CommonPacket pack) {
		SimpleMsgItem st = new SimpleMsgItem(pack);
		st.setPanelClickedEvent((e)->{
			if(oldItem!=null) {
				oldItem.setSelected(false);
			}
			oldItem = st;
			st.setSelected(true);
			detailMsgPanel.setPack(st.getPack());
			byteMsgPanel.setPack(st.getPack());
		});
		simpleMsgPanel.add(st);
		simpleMsgPanel.validate();
		simpleMsgPanel.repaint();
		simpleMsgPanel.revalidate();
	}

	public static YPSniffUI getInstance() {
		if (self == null) {
			self = new YPSniffUI();
		}
		return self;
	}

	@Override
	public void run() {
		// TODO Auto-generated method stub
		self.setVisible(true);
	}

	public Dimension getSize() {
		return size;
	}

	public void setSize(Dimension size) {
		this.size = size;
	}

	public YPanel getSimpleMsgPanel() {
		return simpleMsgPanel;
	}

	public void setSimpleMsgPanel(YPanel simpleMsgPanel) {
		this.simpleMsgPanel = simpleMsgPanel;
	}

	public DetailMsgPanel getDetailMsgPanel() {
		return detailMsgPanel;
	}

	public void setDetailMsgPanel(DetailMsgPanel detailMsgPanel) {
		this.detailMsgPanel = detailMsgPanel;
	}

	public ByteMsgPanel getByteMsgPanel() {
		return byteMsgPanel;
	}

	public void setByteMsgPanel(ByteMsgPanel byteMsgPanel) {
		this.byteMsgPanel = byteMsgPanel;
	}

	public FuncPanel getFuncPanel() {
		return funcPanel;
	}

	public void setFuncPanel(FuncPanel funcPanel) {
		this.funcPanel = funcPanel;
	}

	public XPanel getSimpleMsgTitlePanel() {
		return simpleMsgTitlePanel;
	}

	public void setSimpleMsgTitlePanel(XPanel simpleMsgTitlePanel) {
		this.simpleMsgTitlePanel = simpleMsgTitlePanel;
	}
}
